package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.ProdAuthCtrlEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.ProdAuthCtrlBean;
import cc.rengu.igas.bomp.facade.bean.ProdFeeInfoBean;
import cc.rengu.igas.bomp.facade.bean.ProdStageInfoBean;
import cc.rengu.igas.bomp.facade.request.ProdInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.ProdInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 产品详细信息查询服务
 * Created by 王成 on 2020/3/9
 */
public class ProdInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ProdInfoQueryRequest prodInfoQueryRequest = new ProdInfoQueryRequest();
        ConvertUtil.convertOutput(prodInfoQueryRequest);
        return prodInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ProdInfoQueryRequest prodInfoQueryRequest = (ProdInfoQueryRequest) request;
        ProdInfoQueryResponse prodInfoQueryResponse = new ProdInfoQueryResponse();
        BeanUtil.beanCopy(prodInfoQueryRequest, prodInfoQueryResponse);
        BizResponse<ProdInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        String instId = prodInfoQueryRequest.getHeader().getInstId();

        //基础
        if ("00".equals(prodInfoQueryRequest.getProdType())
                || "DF".equals(prodInfoQueryRequest.getProdCode())) {
            //WEB前台有bug
            prodInfoQueryRequest.setProdType("00");
            BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
            BaseProdInfo baseProdInfo = baseProdInfoMapper.selectBaseProdInfoByPrimaryKey(instId, prodInfoQueryRequest.getBaseProdCode());
            if (null == baseProdInfo) {
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setResult(prodInfoQueryResponse);
                return bizResponse;
            }
            prodInfoQueryResponse.setAlipayChannelId(baseProdInfo.getAlipayChannelId());
            prodInfoQueryResponse.setWechatChannelId(baseProdInfo.getWechatChannelId());

            //TODO 银联商户号？

            /* 获取产品费率配置信息 */
            ProdFeeInfoMapper prodFeeInfoMapper = new ProdFeeInfoMapperImpl();
            List<ProdFeeInfo> prodFeeInfoList = prodFeeInfoMapper.selectProdFeeInfoByBaseProdCode(instId, prodInfoQueryRequest.getProdType(), prodInfoQueryRequest.getBaseProdCode());
            if (null != prodFeeInfoList && !prodFeeInfoList.isEmpty()) {
                List<ProdFeeInfoBean> prodFeeInfoBeans = new ArrayList<>();
                prodFeeInfoList.forEach(item -> convProdFeeInfo(item, prodFeeInfoBeans));
                prodInfoQueryResponse.setProdFeeInfoList(prodFeeInfoBeans);
            }

            /* 获取分期产品信息 */
            ProdStageInfoMapper prodStageInfoMapper = new ProdStageInfoMapperImpl();
            List<ProdStageInfo> prodStageInfoList = prodStageInfoMapper.selectProdStageInfoByProdType(instId, prodInfoQueryRequest.getProdType(), prodInfoQueryRequest.getBaseProdCode());
            if (null != prodStageInfoList && !prodStageInfoList.isEmpty()) {
                List<ProdStageInfoBean> prodStageInfoBeans = new ArrayList<>();
                prodStageInfoList.forEach(item -> convProdStageInfo(item, prodStageInfoBeans));
                prodInfoQueryResponse.setProdStageInfoList(prodStageInfoBeans);
            }

            /* 获取产品业务权限信息 */
            ProdAuthCtrlMapper prodAuthCtrlMapper = new ProdAuthCtrlMapperImpl();
            List<ProdAuthCtrl> prodAuthCtrlList = prodAuthCtrlMapper.selectProdAuthCtrlInfoByBaseProdCode(instId, prodInfoQueryRequest.getProdType(), prodInfoQueryRequest.getBaseProdCode());
            if (null != prodAuthCtrlList && !prodAuthCtrlList.isEmpty()) {
                List<ProdAuthCtrlBean> prodAuthCtrlBeans = new ArrayList<>();
                prodAuthCtrlList.forEach(item -> convProdAuthCtrl(item, prodAuthCtrlBeans));
                prodInfoQueryResponse.setProdAuthCtrlList(prodAuthCtrlBeans);
            }
        } else if ("01".equals(prodInfoQueryRequest.getProdType())) {
            //标准
            ProdInfoMapper prodInfoMapper = new ProdInfoMapperImpl();
            ProdInfo prodInfo = prodInfoMapper.selectProdInfoByPrimaryKey(instId, prodInfoQueryRequest.getProdCode(), prodInfoQueryRequest.getBaseProdCode());
            if (null == prodInfo) {
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setResult(prodInfoQueryResponse);
                return bizResponse;
            }

            prodInfoQueryResponse.setAlipayChannelId(prodInfo.getAlipayChannelId());
            prodInfoQueryResponse.setWechatChannelId(prodInfo.getWechatChannelId());

            //TODO 银联商户号？

            /* 获取产品费率配置信息 */
            ProdFeeInfoMapper prodFeeInfoMapper = new ProdFeeInfoMapperImpl();
            List<ProdFeeInfo> prodFeeInfoList = prodFeeInfoMapper.selectProdFeeInfoByBaseProdCodeAndProdCode(instId, prodInfoQueryRequest.getBaseProdCode(), prodInfoQueryRequest.getProdCode());
            if (null != prodFeeInfoList && !prodFeeInfoList.isEmpty()) {
                List<ProdFeeInfoBean> prodFeeInfoBeans = new ArrayList<>();
                prodFeeInfoList.forEach(item -> convProdFeeInfo(item, prodFeeInfoBeans));
                prodInfoQueryResponse.setProdFeeInfoList(prodFeeInfoBeans);
            }

            /* 获取分期产品信息 */
            ProdStageInfoMapper prodStageInfoMapper = new ProdStageInfoMapperImpl();
            List<ProdStageInfo> prodStageInfoList = prodStageInfoMapper.selectProdStageInfoByProdCode(instId, prodInfoQueryRequest.getProdType(), prodInfoQueryRequest.getBaseProdCode(), prodInfoQueryRequest.getProdCode());
            if (null != prodStageInfoList && !prodStageInfoList.isEmpty()) {
                List<ProdStageInfoBean> prodStageInfoBeans = new ArrayList<>();
                prodStageInfoList.forEach(item -> convProdStageInfo(item, prodStageInfoBeans));
                prodInfoQueryResponse.setProdStageInfoList(prodStageInfoBeans);
            }

            /* 获取产品业务权限信息 */
            ProdAuthCtrlMapper prodAuthCtrlMapper = new ProdAuthCtrlMapperImpl();
            List<ProdAuthCtrl> prodAuthCtrlList = prodAuthCtrlMapper.selectProdAuthCtrlInfoByProdCode(instId, prodInfoQueryRequest.getProdType(), prodInfoQueryRequest.getBaseProdCode(), prodInfoQueryRequest.getProdCode());
            if (null != prodAuthCtrlList && !prodAuthCtrlList.isEmpty()) {
                List<ProdAuthCtrlBean> prodAuthCtrlBeans = new ArrayList<>();
                prodAuthCtrlList.forEach(item -> convProdAuthCtrl(item, prodAuthCtrlBeans));
                prodInfoQueryResponse.setProdAuthCtrlList(prodAuthCtrlBeans);
            }

        }

        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(prodInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ProdInfoQueryResponse prodInfoQueryResponse = (ProdInfoQueryResponse) bizResponse.getResult();
        List<ProdAuthCtrlBean> prodAuthCtrlBeans = prodInfoQueryResponse.getProdAuthCtrlList();

        /**
         * 银行卡支付-快捷支付：
         * 1、本行卡快捷支付(P01PM004 00)、新卡支付(本行卡)(P01PM001 00)、银行卡首次签约并支付(本行卡)(P01PM011 00)，后两个产品隐藏，并且增加与第一个产品的关联关系，即选择本行卡快捷支付时，后两个产品同步开通。
         * 2、银联无跳转支付(P01PM004 01)、银联无跳转签约并支付(P01PM013 00)、银联无跳转首次签约并支付(P01PM013 01)，参照第一条处理
         * 3、银联无卡快捷支付(P01PM004 02)、新卡支付(银联无卡快捷)(P01PM001 01)、银行卡首次签约并支付(银联无卡快捷)(P01PM011 01)，参照第一条处理
         * 【前台修改，后台无需处理】4、开通本行个人网关支付(P01PM007 00)、本行企业网关支付(P01PM007 01) ，默认开通本行网银支付API(P01CP241 00) ；
         * 本行个人网关支付退款(P04PM007 00)，本行企业网关支付退款(P04PM007 01) ，默认开通本行网银支付退款API(P04CP241 00)  ；
         * 本行网银支付API(P04PM007 00)、本行网银支付退款API(P04CP241 00)可独立开通
         * 5、收银台微信公众号支付(P01PM002 00)，默认开通 微信公众号支付API(P01CP501 00) ；收银台微信小程序支付(P01PM002 01) ，默认开通微信小程序支付API(P01CP502 00)；微信公众号支付API(P01CP501 00)、微信小程序支付API(P01CP502 00)均隐藏
         */
        if (!CollectionUtils.isEmpty(prodAuthCtrlBeans)) {
            Iterator iterator = prodAuthCtrlBeans.iterator();
            while (iterator.hasNext()) {
                ProdAuthCtrlBean item = (ProdAuthCtrlBean) iterator.next();
                ProdAuthCtrlEnum prodAuthCtrlEnum = ProdAuthCtrlEnum.getProdEnumByTxnNumBizType(item.getTxnNum(), item.getBizType());
                if (null == prodAuthCtrlEnum || prodAuthCtrlEnum.isShowFlag()) {
                    //为空或展示的数据跳过
                    continue;
                }
                //隐藏的数据，不返回给前台
                iterator.remove();
            }
        }
        ConvertUtil.convertInput(prodInfoQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param prodFeeInfo      数据库表结构对象
     * @param prodFeeInfoBeans 接口对象
     */
    private void convProdFeeInfo(ProdFeeInfo prodFeeInfo, List<ProdFeeInfoBean> prodFeeInfoBeans) {
        ProdFeeInfoBean prodFeeInfoBean = new ProdFeeInfoBean();
        BeanUtil.beanCopy(prodFeeInfo, prodFeeInfoBean);
        prodFeeInfoBeans.add(prodFeeInfoBean);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param prodStageInfo      数据库表结构对象
     * @param prodStageInfoBeans 接口对象
     */
    private void convProdStageInfo(ProdStageInfo prodStageInfo, List<ProdStageInfoBean> prodStageInfoBeans) {
        ProdStageInfoBean prodStageInfoBean = new ProdStageInfoBean();
        BeanUtil.beanCopy(prodStageInfo, prodStageInfoBean);
        prodStageInfoBean.setRecordStatus(prodStageInfo.getStageStatus());
        prodStageInfoBeans.add(prodStageInfoBean);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param prodAuthCtrl      数据库表结构对象
     * @param prodAuthCtrlBeans 接口对象
     */
    private void convProdAuthCtrl(ProdAuthCtrl prodAuthCtrl, List<ProdAuthCtrlBean> prodAuthCtrlBeans) {
        ProdAuthCtrlBean prodAuthCtrlBean = new ProdAuthCtrlBean();
        BeanUtil.beanCopy(prodAuthCtrl, prodAuthCtrlBean);
        prodAuthCtrlBeans.add(prodAuthCtrlBean);
    }
}
